package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_MARRIAGE_APPLY_PUBLISH, desc = "发布联姻请求")
public class MarriageApplyPublishCmd extends AbstractCommand {

    private boolean isAllPublish;
    private String childUid;
    private long appointUserId;
    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MarriageProto.MarriageApplyPublishReqMsg reqMsg = MarriageProto.MarriageApplyPublishReqMsg.parseFrom(packet.getBytes());
        isAllPublish = reqMsg.getIsAllPublish();
        childUid = reqMsg.getChildUid();
        appointUserId = reqMsg.getAppointUserId();
        type = reqMsg.getApplyType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(appointUserId > 0 && appointUserId == player.getUserId()){
            ret = GameErrorCode.E_MARRIAGE_NO_SELF;
            return;
        }
        ret = MarriageMgr.publishMarriageApply(player, childUid, appointUserId, isAllPublish, type);
    }

    @Override
    public void sendData(GamePlayer player) {
        boolean needResponse = true;
        if(!isAllPublish && 1 == type){
            if(0 == ret){//商会联姻发到跨服了
                needResponse = false;
            }
        }
        if(ret == -1){
            needResponse = false;
        }
        if(needResponse) {
            MarriageProto.MarriageApplyPublishRespMsg.Builder resp = MarriageProto.MarriageApplyPublishRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(Protocol.U_MARRIAGE_APPLY_PUBLISH, resp);
        }
    }
}
